#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

// 域名一次递归查找父域名，a.b.c.d.e.com.
//  依次查找a.b.c.d.e.com.  ----->b.c.d.e.com.---->c.d.e.com.--->d.e.com.--->e.com.
void dp_domain_recursive(const std::string data) {
    if(data.empty()) {
        return ;
    }
    size_t pos = 0;
    size_t pos_last = data.size()-1;
    const char * str = data.c_str();
    while(1) {
        cout << str+pos << endl;
        pos = data.find('.',pos);
        if(pos!=std::string::npos) {
            pos += 1;
            if (pos != pos_last) {
                while (data[pos] == '.') {
                    pos += 1;
                }
            } else {
                break;
            }
        } else {
            break;
        }
    }
}

// 测试data 是否以suffix结尾
bool dp_end_with(const std::string& data, const std::string& suffix){
    if (data.empty() || suffix.empty() || data.length() < suffix.length()) {
        return false;
    }
    size_t pos = data.rfind(suffix);
    if (pos != std::string::npos && pos == (data.size() - suffix.size())) {
        return true;
    }
    return false;
}

//测试删除最后一个字符
void test_erase_last_char()
{
    std::string q_domain_ = "aa.bb.cc.";
    if (!q_domain_.empty() && '.' == q_domain_.at(q_domain_.size() - 1)) {
        q_domain_.erase(q_domain_.size() - 1, 1);
    }
    cout << "test result=" << q_domain_ << endl;
}

// 测试预分配空间
void test_string_capacity()
{
	{
		std::string body(8192, 0);
		printf("body.size=%lu, body.length=%lu, capacity=%lu\n", body.size(),
				body.length(), body.capacity());
	}
	{
		std::string body;
		body.reserve(8192);
		printf("body.size=%lu, body.length=%lu, capacity=%lu\n", body.size(),
				body.length(), body.capacity());
	}
}

int main()
{
	test_string_capacity();
	return 0;

    std::string reverse("......a.b.c.d.e.com.......");
    dp_domain_recursive(reverse);
    reverse = "aaaa";
    dp_domain_recursive(reverse);

    std::string data("aaa.bbb.ccc.com.");
    std::string suffix(".ccc.com.");
    cout << "检测`" << data << "`是否以`" << suffix << "`结尾的结果是:" << dp_end_with(data, suffix) <<endl;
    test_erase_last_char();
    string str("There are two needles in this haystack with needles.");
    string str2("needle");
    size_t found;

    // different member versions of find in the same order as above:
    found = str.find(str2);
    if (found != string::npos)
        cout << "first 'needle' found at: " << int(found) << endl;

    found = str.rfind("needles are small", found + 1, 0);
    cout << "whb found" << found << endl;

    found = str.find("needles are small", 0, 8);
    if (found != string::npos) {
        cout << "second 'needle' found at: " << int(found) << endl;
    }

    found = str.find("haystack");
    if (found != string::npos)
        cout << "'haystack' also found at: " << int(found) << endl;

    found = str.find('.');
    if (found != string::npos)
        cout << "Period found at: " << int(found) << endl;

    // let's replace the first needle:
    str.replace(str.find(str2), str2.length(), "preposition");
    cout << str << endl;

    return 0;
}
